<?php
/**
 *
 * @category   babel 
 * @package    BABEL
 * @author     Hannes Magnusson <hannes.magnusson@gmail.com>
 * @copyright  2008 Team babel
 * @license    LGPL
 */


define("BABEL_DEFAULT_ERRMSG", "This page is temporary unavailable, please try again later");

// FC For PHP5.3 and 5.2
if (!defined("E_RECOVERABLE_ERROR")) {
	define("E_RECOVERABLE_ERROR",    E_STRICT               << 1);
	define("E_DEPRECATED",           E_RECOVERABLE_ERROR    << 1);
} elseif (!defined("E_DEPRECATED")) {
	define("E_DEPRECATED",           E_RECOVERABLE_ERROR    << 1);
}


function ex_handler($ex) {

	$a = $ex->getTrace();
	$t = $a[0];

	if (_ICE_DEBUG) {
		debug_error($ex->getMessage() . "<br />\n" . nl2br($ex->getTraceAsString()), $t["file"], $t["line"]);
	}
	$class = isset($t["class"]) ? $t["class"] : null;
	if (isset($GLOBALS['obj_log'])) {
		//@$GLOBALS['obj_log']->err($ex->getMessage(), "global_error_handling", $t["file"], $class, $t["function"], $t["line"]);
	}

	if ($ex instanceof UserException) {
		return trigger_error($e->getUserMessage(), E_USER_ERROR);
	}
	trigger_error(BABEL_DEFAULT_ERRMSG, E_USER_ERROR);
}

function err_handler($errno, $errstr, $errfile, $errline, $errctx) {
	/* Singleton anyone? */
	global $obj_tpl;
	static $err = false;

	if ($err) {
		/* Recursion protection */
		return true;
	}

	$err = true;

	switch($errno) {
	/* Babel triggered errors */
	case E_USER_ERROR:
	case E_USER_WARNING:
	case E_USER_NOTICE:
		if (_ICE_DEBUG) {
			debug_error($errstr, $errfile, $errline);
		}

		pretty_error($errstr);

		include('includes/babel/append.php');
		exit(1);
		break;

	/* PHP triggered errors */
	case E_DEPRECATED:
	case E_RECOVERABLE_ERROR:
	case E_STRICT:
	case E_WARNING:
	case E_NOTICE:
		/* Minor programming mistakes */
		if (_ICE_DEBUG) {
			debug_error($errstr, $errfile, $errline);
		}
		break;

	default:
		/* Unknown error level.. let PHP handle it */
		if (_ICE_DEBUG) {
			var_dump($errno, $errstr);
		}
		$err = false;
		return false;
	}

	$err = false;
	return true;
}

function debug_error($msg, $file, $line) {
	$msg = sprintf('<div class="error"><strong>%s:%d</strong><p>%s</p></div>', $file, $line, $msg);
	Babel_Error_Logger::push($msg);
}

function pretty_error($errmsg = null) {
	if (!$errmsg) {
		$errmsg = BABEL_DEFAULT_ERRMSG;
	}

	/* Why don't we use singleton? */
	if (isset($GLOBALS["obj_tpl"])) {
		$o = $GLOBALS["obj_tpl"];
		$o->setFile("error", "babel/errorpage.html");
		$o->setVar("VAR_SERIOUS_ERROR", $errmsg);
		$o->pparse("error", "error");
	}
}

class Babel_Error_Logger {
	private static $errors = array();

	public static function push($err) {
		if (is_scalar($err)) {
			self::$errors[] = $err;
			return;
		}
		if (is_object($err) || is_array($err)) {
			self::$errors[] = print_r($err, true);
			return;
		}

		throw new Exception("Unknown type: " . gettype($err));
	}
	public static function pop() {
		return array_pop(self::$errors);
	}
	public static function fetch() {
		return self::$errors;
	}
}

set_exception_handler("ex_handler");
set_error_handler("err_handler");


